package fmu.pi.view;

import java.io.IOException;
import java.io.InputStream;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import fmu.pi.view.controllers.LoginController;
import fmu.pi.view.controllers.MainController;

/**
 * @author valdir.c.junior
 * 
 * Classe responsável por iniciar o sistema SNL
 *
 */
public class Main extends Application {

	
	private Stage stage;
	
	/**
	 * JavaFX application Starter
	 * @see javafx.application.Application#start(javafx.stage.Stage)
	 */
	@Override
	public void start(Stage primaryStage) {
		startFXMLApp(primaryStage);
	}

	/**
	 * JVM starter
	 * @param args
	 */
	public static void main(String[] args) {
		launch(args);
	}
	
	
	/**
	 * Application Loader
	 * @param primaryStage
	 */
	private void startFXMLApp(Stage primaryStage){
			
		this.stage = primaryStage;
		
		stage.setResizable(false);
		stage.setTitle("$ó no lucro 1.0");
		
		try {
			
			String fxml = "/fmu/pi/view/fxml/login.fxml";
			InputStream in = Main.class.getResourceAsStream(fxml);
			
			FXMLLoader loader = new FXMLLoader();
			loader.setBuilderFactory(new JavaFXBuilderFactory());
			loader.setLocation(Main.class.getResource(fxml));

			Pane pane = null;
			
			try {
				
				pane = (Pane) loader.load(in);
				LoginController ctrl = (LoginController) loader.getController();
				
				if (ctrl.isBaseEmUso()) {
					pane = gerarTelaErroLogin();
					stage.setScene(new Scene(pane, 340, 240));
					
				} else {
					ctrl.setApp(this);
					stage.setScene(new Scene(pane, 400, 300));
					
				} 
			
			} finally {
				in.close();
			}
			
			stage.show();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * Metodo responsável por gerar a tela de erro em caso de DBLOCK
	 * 
	 * @return Pane contendo a tela de erro
	 * @throws IOException erro
	 */
	private Pane gerarTelaErroLogin() throws IOException{
		
		String fxml = "/fmu/pi/view/fxml/erro.fxml";
		InputStream in = Main.class.getResourceAsStream(fxml);
		
		FXMLLoader loader = new FXMLLoader();
		loader.setBuilderFactory(new JavaFXBuilderFactory());
		loader.setLocation(Main.class.getResource(fxml));
		
		try {
			return (Pane) loader.load(in);
			
		} finally {
			in.close();
		}
	}
	
	
	
	/**
	 * Metodo que efetua 
	 */
	public void renderizeMainForm() {
		
		try {
			
			String fxml = "/fmu/pi/view/fxml/main.fxml";
			InputStream in = Main.class.getResourceAsStream(fxml);
			Pane pane = null;
			
			try {
				
				FXMLLoader loader = new FXMLLoader();
				loader.setBuilderFactory(new JavaFXBuilderFactory());
				loader.setLocation(Main.class.getResource(fxml));
						
				pane = (Pane) loader.load(in);
				MainController ctrl = (MainController) loader.getController();
				ctrl.setApp(this);
				
			} finally {
				in.close();
			}	
			
			stage.setScene(new Scene(pane, 900, 650));
	        stage.setResizable(true);
	        stage.centerOnScreen();       
	        stage.show(); 
	        
		} catch (Exception e) {
			e.printStackTrace();
		
		}		
	}
}
